package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageParam;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderMemberSetmealVo;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Author: SL
 * @Date: 2020/9/25 18:34
 */
/*
    后端预约设置
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;


    //查询会员与预约数据
    @RequestMapping("/findOrderAndMemberById")
    public Result findOrderAndMemberById(Integer orderId) {
        try {
            OrderMemberSetmealVo orderMemberSetmealVo = orderService.findOrderAndMemberById(orderId);
            return new Result(true, MessageConstant.GET_MEMBER_ORDER_SETMEAL_SUCCESS, orderMemberSetmealVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_ORDER_SETMEAL_FAIL);
        }
    }

    //编辑预约信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderMemberSetmealVo oms, Integer setmealId) {
        try {
            oms.setSetmealId(setmealId);
            return orderService.edit(oms, setmealId);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDER_FAIL);
        }
    }

    //删除预约信息
    @RequestMapping("/delete")
    public Result deleteOrder(Integer orderId) {
        try {
            orderService.deleteOrder(orderId);
            return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
        }
    }

    //新增预约
    @RequestMapping("/addOrder")
    public Result addOrder(@RequestBody Map map, String setmealId) {
        try {
            map.put("setmealId", setmealId);
            return orderService.addOrder(map);
            // return new Result(true,MessageConstant.ADD_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ORDER_FAIL);
        }
    }

    //按钮修改预约状态
    @RequestMapping("/changeStatus")
    public Result changeStatus(@RequestBody Map<String, Object> map) {
        try {
            Integer orderId = (Integer) map.get("orderId");
            String orderStatus = (String) map.get("orderStatus");
            orderService.changeStatus(orderId, orderStatus);
            return new Result(true, MessageConstant.EDIT_ORDERSTATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_ORDERSTATUS_FAIL);
        }
    }


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageParam param) {
        return orderService.findPage(param);

    }

}
